package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itheima.pojo.Menu;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@RestController
@RequestMapping("/user")
public class UserController {


    @Reference
    private UserService userService;

    /**
     * 获取用户的用户名
     *
     * @return
     */
   /*@RequestMapping("/getUsername")
   public Result getUsername() {

      try {
         User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
         return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
      } catch (Exception e) {
         e.printStackTrace();
         return new Result(false, MessageConstant.GET_USERNAME_FAIL);
      }
   }*/
    @RequestMapping("/getUsername")
    public Result getUsername() {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

   /*
   {
      value: '选项1',
      label: '黄金糕'
   }
    */

    /**
     * 查询所有健康管理师的名字
     * @return
     */
    @RequestMapping("/findAllHealthManager")
    public Result findAllHealthManager() {
        try {
            List<Map<String, Object>> list = userService.findAllHealthManager();
            return new Result(true, MessageConstant.QUERY_HEALTHMANAGER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_HEALTHMANAGER_FAIL);
        }
    }

    /**
     * 通过会员id查询健康管理师id
     * @param memberId
     * @return
     */
    @RequestMapping("/findByMemberId/{memberId}")
    public Result findByMemberId(@PathVariable Integer memberId) {
        try {
            Integer id = userService.findByMemberId(memberId);
            return new Result(true, MessageConstant.QUERY_HEALTHMANAGER_SUCCESS,id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_HEALTHMANAGER_FAIL);
        }
    }


    /**
     * 1.获取用户的用户名以及菜单信息
     *
     * @return
     */
    @RequestMapping("/detail")
    public Result detail() {
        try {
            HashMap<String, Object> result = new HashMap<>();
            // 返回用户信息:
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            result.put("username", user.getUsername());
            // 返回菜单列表信息
            List<Menu> list = userService.getMenuByUsername(user.getUsername());
            result.put("menuList", list);

            // 统计用户登录次数
            userService.loginStatistics(user.getUsername());
            return new Result(true, MessageConstant.GET_USERINFO_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERINFO_FAIL);
        }
    }

    /**
     * 2.用户列表分页+模糊查询
     *
     * @param pageBean
     * @return
     */
    @PostMapping("/pagination")
    public Result pagination(@RequestBody QueryPageBean pageBean) {
        PageResult result = null;
        try {
            result = userService.getUserList(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USER_FAIL);
        }
        return new Result(true, MessageConstant.GET_USER_SUCCESS, result);
    }

    /**
     * 3.添加后台用户
     *
     * @param roleIds
     * @param user
     * @return
     */
    @PostMapping("/create/{roleIds}")
    public Result create(@PathVariable Integer[] roleIds, @RequestBody com.itheima.pojo.User user) {

        try {
            return userService.create(roleIds, user);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 4.根据用户id查询用户信息以及关联的角色信息
     *
     * @param id
     * @return
     */
    @GetMapping("/preUpdate/{id}")
    public Result preUpdate(@PathVariable Integer id) {

        Map<String, Object> result = null;
        try {
            result = userService.getUserById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USER_FAIL);
        }
        return new Result(true, MessageConstant.GET_USER_FAIL, result);
    }


    @PostMapping("/update/{roleIds}")
    public Result update(@PathVariable Integer[] roleIds, @RequestBody com.itheima.pojo.User user) {
        try {
            return userService.update(roleIds, user);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        try {
            userService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }

}
